package com.bestv.search.management.delegate;

import com.bestv.search.common.dataSource.DataSourceKey;
import com.bestv.search.common.dataSource.DataSourceKeyUtil;
import com.bestv.search.common.model.DynamicMsg;
import com.bestv.search.common.model.KeySubject;
import com.bestv.search.common.service.DynamicMsgManager;
import org.springframework.aop.AfterReturningAdvice;

import java.lang.reflect.Method;
import java.util.Date;

public class KeySubjectDelegate implements AfterReturningAdvice {

    DynamicMsgManager dmManager;

    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        if (method.getName() == null || !method.getName().startsWith("delete") || returnValue == null
                || returnValue.getClass() != KeySubject.class)
            return;
        KeySubject ks = (KeySubject) returnValue;
        DynamicMsg dm = new DynamicMsg();
        dm.setClassName(KeySubject.class.getName());
        dm.setDealed(false);
        dm.setObjId(ks.getId());
        dm.setOperateType((byte) 0);// delete
        dm.setUpdateTime(new Date());

        DataSourceKey key = DataSourceKeyUtil.convert(ks.getSite().getCode());
        dmManager.saveDynamicMsg(key, dm);
    }

    public DynamicMsgManager getDmManager() {
        return dmManager;
    }

    public void setDmManager(DynamicMsgManager dmManager) {
        this.dmManager = dmManager;
    }

}
